﻿using System;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Media;
using OpenLS.Spreadsheet.Internals;

namespace OpenLS.Spreadsheet.UI
{
    internal class FontFormatter : FormatterBase
    {
        private string _fontFamily = "Arial";
        private FontStyle? _fontStyle;
        private FontWeight? _fontWeight;
        private bool _isNormal;
        private double? _size;
        private UnderlineType? _underline;
        private SpreadsheetColor _foreground;

        public FontFormatter(DifferentialStyleRecord record)
        {
            if (record.Font != null)
            {
                Font font = record.Font;
                if (font.FontFamily != null)
                    FontFamily = font.FontFamily.Source;
                Size = font.Size;
                FontStyle = font.FontStyle;
                FontWeight = font.FontWeight;
                Underline = font.Underline;
            }
            IsDirty = false;
            
        }
        public FontFormatter(FormattingRecord record)
        {
            Contract.Requires(record != null);
            if (record.Font != null)
            {
                Font font = record.Font;
                if (font.FontFamily != null)
                    FontFamily = font.FontFamily.Source;
                Size = font.Size;
                FontStyle = font.FontStyle;
                FontWeight = font.FontWeight;
                Underline = font.Underline;
            }
            IsDirty = false;
        }

        public UnderlineType? Underline
        {
            get { return _underline; }
            set
            {
                _underline = value;
                NotifyDirtyPropertyChanged("Underline");
            }
        }

        public FontWeight? FontWeight
        {
            get { return _fontWeight; }
            set
            {
                _fontWeight = value;
                NotifyDirtyPropertyChanged("FontWeight");
            }
        }

        public double? Size
        {
            get { return _size; }
            set
            {
                _size = value;
                NotifyDirtyPropertyChanged("Size");
            }
        }

        public string FontFamily
        {
            get { return _fontFamily; }
            set
            {
                _fontFamily = value;
                NotifyDirtyPropertyChanged("FontFamily");
            }
        }

        public FontStyle? FontStyle
        {
            get { return _fontStyle; }
            set
            {
                _fontStyle = value;
                NotifyDirtyPropertyChanged("FontStyle");
            }
        }

        public bool IsNormal
        {
            get { return _isNormal; }
            set
            {
                if (value)
                    Size = 12;
                FontWeight = FontWeights.Normal;
                FontStyle = FontStyles.Normal;
                Underline = UnderlineType.None;
                _isNormal = value;
                NotifyDirtyPropertyChanged("IsNormal");
            }
        }
        public int[] FontSizes { get { return new[] { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 }; } }


        public SpreadsheetColor Foreground
        {
            get {
                return _foreground;
            }
            set {
                _foreground = value;this.NotifyDirtyPropertyChanged("Foreground");
            }
        }

        protected override void ApplyOverride(Range selection)
        {
            if (FontFamily != null)
                selection.FontFamily = new FontFamily(FontFamily);
            if (FontStyle != null)
            selection.FontStyle = FontStyle;
            if (this.FontWeight != null)
            selection.FontWeight = FontWeight;
            if (Underline != null)
            selection.FontUnderline = Underline;
            if (Size != null)
            selection.FontSize = Size;
            if (Foreground != null)
                selection.Foreground = Foreground;
        }

        public void SetStyle(int selectedIndex)
        {
            IsNormal = false;
            switch (selectedIndex)
            {
                case 0:
                    FontWeight = FontWeights.Normal;
                    FontStyle = FontStyles.Normal;
                    break;
                case 1:
                    FontWeight = FontWeights.Normal;
                    FontStyle = FontStyles.Italic;

                    break;
                case 2:
                    FontWeight = FontWeights.Bold;
                    FontStyle = FontStyles.Normal;
                    break;
                case 3:
                    FontWeight = FontWeights.Bold;
                    FontStyle = FontStyles.Italic;
                    break;
            }
        }

        
        public void ApplyRule(StandardConditionalFormattingRule rule)
        {
            if (IsDirty)
            {
                rule.Font = new Font(f =>
                                         {
                                             if (FontFamily != null)
                                                 f.FontFamily = new FontFamily(FontFamily);
                                             if (FontStyle != null)
                                                 f.FontStyle = FontStyle.Value;
                                             if (this.FontWeight != null)
                                                 f.FontWeight = FontWeight.Value;
                                             if (Underline != null)
                                                 f.Underline = Underline.Value;
                                             if (Size != null)
                                                 f.Size = Size.Value;
                                             if (Foreground != null)
                                                 f.Foreground = Foreground;
                                         });
            }
        }
    }
}